﻿using CarFileManager;
using CarFileManager.Models;
using CarManager.Commands;
using Microsoft.Win32;

namespace CarManager.ViewModels
{
    class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            OpenFileCommand = new DelegateCommand(OpenFileCommandExecuted);
            
            AddCommand = new DelegateCommand(AddCommandExecuted);
            SaveCommand = new DelegateCommand(SaveCommandExecuted);
            DeleteCommand = new DelegateCommand(DeleteCommandExecuted);
            
            FirstCommand = new DelegateCommand(FirstCommandExecuted);
            PreviousCommand = new DelegateCommand(PreviousCommandExecuted);
            NextCommand = new DelegateCommand(NextCommandExecuted);
            LastCommand = new DelegateCommand(LastCommandExecuted);
        }
        
        /// <summary>
        /// The OpenFileCommand function.
        /// </summary>
        private void OpenFileCommandExecuted()
        {
            var ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == true)
            {
                _fileManager = new XmlCarFileManager(ofd.FileName);

                SelectedFile = _fileManager.FileName;
                CurrentCar = _fileManager.CurrentCar;
            }
        }

        private void AddCommandExecuted()
        {
            // _fileManager.Add();
        }
       
        private void SaveCommandExecuted()
        {
            // _fileManager.Save();
        }

        private void DeleteCommandExecuted()
        {
            // _fileManager.Delete();
        }

        private void FirstCommandExecuted()
        {
            _fileManager.First();
            CurrentCar = _fileManager.CurrentCar;
        }

        private void PreviousCommandExecuted()
        {
            _fileManager.Previous();
            CurrentCar = _fileManager.CurrentCar;
        }
        
        private void NextCommandExecuted()
        {
            _fileManager.Next();
            CurrentCar = _fileManager.CurrentCar;
        }

        private void LastCommandExecuted()
        {
            _fileManager.Last();
            CurrentCar = _fileManager.CurrentCar;
        }

        private CarFileManagerBase _fileManager;

        private string _selectedFile;
        public string SelectedFile
        {
            get { return _selectedFile; }
            set
            {
                _selectedFile = value;
                OnPropertyChanged();
            }
        }

        private ICar _currentCar;
        public ICar CurrentCar
        {
            get { return _currentCar; }
            set
            {
                _currentCar = value;
                OnPropertyChanged();
            }
        }

        #region Commands

        /// <summary>
        /// Gets the Open File command.
        /// </summary>
        public DelegateCommand OpenFileCommand { get; private set; }

        public DelegateCommand AddCommand { get; private set; }

        public DelegateCommand SaveCommand { get; private set; }

        public DelegateCommand DeleteCommand { get; private set; }

        public DelegateCommand FirstCommand { get; private set; }
        public DelegateCommand PreviousCommand { get; private set; }
        public DelegateCommand NextCommand { get; private set; }
        public DelegateCommand LastCommand { get; private set; }

        #endregion

    }
}
